﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AutoStar
{
    public partial class Nalog : Form
    {
        private int korisnikId;
        private string brojNaloga=null;
        private Form1 glavnaForma;
        private nalog n = new nalog();
        public Nalog(int korisnikId, Form1 glavnaForma)
        {
            InitializeComponent();
            this.korisnikId=korisnikId;
            this.glavnaForma = glavnaForma;
        }

        public Nalog(int korisnikId, Form1 glavnaForma, string brojNaloga)
        {
            InitializeComponent();
            this.korisnikId = korisnikId;
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            AutoStarBaza context = new AutoStarBaza(conn);
            nalog n = (from n1 in context.nalog
                          where n1.broj == brojNaloga
                          select n1).First();
            txtSadrzaj.Text = n.sadrzaj;
            this.brojNaloga = brojNaloga;
            if (n.tipNaloga == 0) rbPregled.Checked = true;
            else rbServis.Checked = true;
            this.glavnaForma = glavnaForma;
            ConnectionPool.checkInConnection(conn);
        }

        private void btnSnimi_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            AutoStarBaza context = new AutoStarBaza(conn);
            if (brojNaloga == null)
            {
                nalog n = new nalog();
                DateTime dt = DateTime.Now;
                n.datum = dt;
                n.korisnikid = korisnikId;
                n.tipNaloga = rbPregled.Checked ? 0 : 1;
                n.sadrzaj = txtSadrzaj.Text;
                n.broj = "xyz";
                context.nalog.InsertOnSubmit(n);
                context.SubmitChanges();
                n.broj = "" + n.id + "-" + dt.ToString("MMyy");
                context.SubmitChanges();
                this.n = n;
                printNalog();
            }
            else
            {
                nalog n = (from n1 in context.nalog
                               where n1.broj == brojNaloga
                               select n1).Single();
                n.tipNaloga = rbPregled.Checked ? 0 : 1;
                n.sadrzaj = txtSadrzaj.Text;
                context.SubmitChanges();
                this.n = n;
                printNalog();
            }
            ConnectionPool.checkInConnection(conn);
            this.Close();
        }

        private void printNalog()
        {
            printDialog.Document = pdNalog;
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                pdNalog.Print();
            }
        }

        private void Nalog_FormClosing(object sender, FormClosingEventArgs e)
        {
            glavnaForma.pretragaNaloga();
        }

        private void btnOtkazi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pdNalog_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString("AutoStar", new Font("Courier New", 24), Brushes.Black, 100, 20);
            e.Graphics.DrawString("Nalog br.: "+n.broj, new Font("Courier New", 14), Brushes.Black, 100, 70);
            e.Graphics.DrawString("Datum: " + n.datum, new Font("Courier New", 14), Brushes.Black, 100, 90);
            e.Graphics.DrawString("Tip naloga: " + (n.tipNaloga==0?"Pregled":"Servis"), new Font("Courier New", 14), Brushes.Black, 100, 110);
            e.Graphics.DrawString("Sadrzaj: ", new Font("Courier New", 14), Brushes.Black, 100, 130);
            int y = 130;
            String sadrzaj = n.sadrzaj.Replace("\n\r", " ");
            sadrzaj = sadrzaj.Replace("\n"," ");

            while(sadrzaj.Length > 40)
            {
                e.Graphics.DrawString(sadrzaj.Substring(0,40), new Font("Courier New", 14), Brushes.Black, 150, y+=20);
                try
                {
                    sadrzaj = sadrzaj.Substring(40, sadrzaj.Length - 40);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            e.Graphics.DrawString(sadrzaj, new Font("Courier New", 14), Brushes.Black, 150, y += 20);
        }
    }
}
